home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / PROGBLER / WASM211B.LZH / FILT1.ASM < prev    next >
Assembly Source File  |  1988-05-26  |  15KB  |  560 lines

  1. ;************************************************
  2. ; Text FILTer
  3. ;
  4. ; Filter routines for FILT.ASM.
  5.  
  6. ;================================================
  7. ; Convert the letter in AL to lower case.
  8.  
  9. Lo_Case PROC    NEAR
  10.         cmp     al, 'A'
  11.         jb      locas1
  12.         cmp     al, 'Z'
  13.         ja      locas1
  14.         add     al, 'a'-'A'
  15. locas1  ret
  16.         ENDP                    ;Lo_Case
  17.  
  18. ;================================================
  19. ; Convert the letter in AL to upper case.
  20.  
  21. Up_Case PROC    NEAR
  22.         cmp     al, 'a'
  23.         jb      upcas1
  24.         cmp     al, 'z'
  25.         ja      upcas1
  26.         sub     al, 'a'-'A'
  27. upcas1  ret
  28.         ENDP                    ;Up_Case
  29.  
  30. ;================================================
  31. ; Store some spaces.
  32. ;
  33. ; In: DX= number of spaces; DI= location.
  34.  
  35. Spaces  PROC  NEAR
  36.         push    ax
  37.         push    cx
  38.         mov     cx, dx          ;load count
  39.         mov     al, ' '         ;space
  40.         rep
  41.         stosb                   ;store bytes
  42.         pop     cx
  43.         pop     ax
  44.         ret
  45.         ENDP                    ;Spaces
  46.  
  47. ;================================================
  48. ; Tab routines.
  49.  
  50. ;------------------------------------------------
  51. ; Clear all tab stops.
  52.  
  53. Tab_Clear PROC  NEAR
  54.         push    ax
  55.         push    cx
  56.         push    di
  57.         sub     al, al          ;zero
  58.         mov     cx, MAXLIN      ;number of entries
  59.         lea     di, TabTbl      ;table location
  60.         rep
  61.         stosb                   ;fill table
  62.         pop     di
  63.         pop     cx
  64.         pop     ax
  65.         ret
  66.         ENDP                    ;Tab_Clear
  67.  
  68. ;------------------------------------------------
  69. ; Set tab stops a every 8 locations.
  70.  
  71. Tab_Reset PROC  NEAR
  72.         call    Tab_Clear       ;clear all tab stops first
  73.  
  74.         push    cx
  75.         push    di
  76.         sub     cx, cx          ;initial column count
  77.         lea     di, TabTbl      ;table location
  78.  
  79. tabres1 cmp     di, TabEnd      ;check if at end
  80.         je      tabres3
  81.         cmp     cx, 8           ;check if at tab stop
  82.         je      tabres2
  83.         inc     cx
  84.         inc     di
  85.         jmps    tabres1
  86.  
  87. tabres2 mov     BYTE [di], 1
  88.         sub     cx, cx
  89.         jmps    tabres1
  90.  
  91. tabres3 pop     di
  92.         pop     cx
  93.         ret
  94.         ENDP                    ;Tab_Reset
  95.  
  96. ;------------------------------------------------
  97. ; Set a tab stop.
  98. ;
  99. ; In: BX= column number.
  100.  
  101. Tab_Set PROC    NEAR
  102.         push    bx
  103.         lea     bx, [TabTbl + bx]       ;get address
  104.         cmp     bx, TabEnd              ;check if past end
  105.         jae     tabset1
  106.         mov     BYTE [bx], 1            ;set to non-zero
  107. tabset1 pop     bx
  108.         ret
  109.         ENDP                            ;Tab_Set
  110.  
  111. ;------------------------------------------------
  112. ; Return the number of spaces to the next tab
  113. ; stop.
  114. ;
  115. ; In: CX= present column number.
  116. ;
  117. ; Out: DX= spaces to next stop; CY= set if at tab
  118. ; tab stop.
  119.  
  120. Tab_Next PROC   NEAR
  121.         push    bx
  122.         push    si
  123.         mov     bx, cx                  ;tab column
  124.         add     bx, TabOff              ;add special offset
  125.         lea     bx, [TabTbl + bx]       ;get starting address
  126.         sub     dx, dx
  127.         cmp     bx, TabEnd              ;check if at or past end
  128.         jae     tabnex3                 ;jump if so
  129.  
  130.         mov     si, bx          ;save starting location
  131.         inc     bx              ;start at next column
  132.         inc     dx              ;set count
  133.  
  134. ;--- loop until tab stop is found or end of table
  135.  
  136. tabnex1 cmp     bx, TabEnd      ;check if end
  137.         jae     tabnex2
  138.         cmp     BYTE [bx], 0    ;check if tab
  139.         jne     tabnex2
  140.         inc     bx              ;next column
  141.         inc     dx              ;increment count
  142.         jmps    tabnex1         ;loop back
  143.  
  144. ;--- found tab stop
  145.  
  146. tabnex2 cmp     BYTE [si], 0    ;check if started at tab stop
  147.         jne     tabnex3
  148.         pop     si
  149.         pop     bx
  150.         clc
  151.         ret
  152.  
  153. ;--- initial tab stop
  154.  
  155. tabnex3 pop     si
  156.         pop     bx
  157.         stc
  158.         ret
  159.         ENDP                    ;Tab_Next
  160.  
  161. ;------------------------------------------------
  162. ; Store a tab character if stored characters.
  163.  
  164. Store_Tab PROC  NEAR
  165.         cmp     SpcCnt, 1       ;check if any spaces to compress
  166.         jbe     stotab2         ;jump if not
  167.         push    ax
  168.         mov     ax, SpcCnt      ;get space count
  169.         cmp     ax, 8           ;check if below 8
  170.         jae     stotab1
  171.         mov     ax, 8           ;set to eight
  172. stotab1 sub     ax, 8           ;reduce for tab
  173.         mov     SpcCnt, ax      ;spaces before tab
  174.         call    Store_Spc       ;store spaces first
  175.         mov     al, TAB         ;load tab
  176.         stosb                   ;store
  177.         inc     cx              ;increment byte count
  178.         dec     TabOff          ;adjust column
  179.         pop     ax
  180. stotab2 ret
  181.         ENDP                    ;Store_Tab
  182.  
  183. ;------------------------------------------------
  184. ; Store the stored spaces.
  185.  
  186. Store_Spc PROC  NEAR
  187.         push    dx
  188.         mov     dx, SpcCnt      ;get number of stored spaces
  189.         or      dx, dx          ;check if any
  190.         jz      stospc1
  191.         call    Spaces          ;store spaces
  192.         add     cx, dx          ;byte count
  193.         sub     TabOff, dx      ;adjust column offset
  194.         mov     SpcCnt, 0       ;reset space count
  195. stospc1 pop     dx
  196.         ret
  197.         ENDP                    ;Store_Spc
  198.  
  199. ;================================================
  200. ; Process a byte.
  201. ;
  202. ; In: AL= character; DI= buffer location; CX=
  203. ; bytes in buffer.
  204. ;
  205. ; Out: CX and DI= updated.
  206.  
  207. Proc_Byte PROC   NEAR
  208.  
  209. ;--- read byte
  210.  
  211.         push    cx
  212.         push    di
  213.         lea     bx, InpBlk      ;input control block
  214.         mov     cx, 1           ;bytes to read
  215.         lea     di, InpBuf      ;input buffer
  216.         call    File_Read       ;read byte
  217.         pop     di
  218.         pop     cx
  219.         jc      probyt5         ;jump if error or EOF
  220.  
  221. ;--- check type of character
  222.  
  223.         mov     al, InpBuf      ;load byte
  224.  
  225. probyt1 cmp     al, 32
  226.         jb      probyt2
  227.         cmp     al, 127
  228.         ja      probyt3
  229.  
  230. ;--- normal byte, 31 < AL < 128
  231.  
  232.         call    Byte_Norm
  233.         ret
  234.  
  235. ;--- low byte, AL < 32
  236.  
  237. probyt2 call    Byte_Low
  238.         ret
  239.  
  240. ;--- high byte, AL > 127
  241.  
  242. probyt3 test    Options, STR_BIT ;check if strip high bit
  243.         jnz     probyt4
  244.         call    Byte_High
  245.         ret
  246.  
  247. probyt4 and     al, 7fh         ;clear bit
  248.         jmps    probyt1         ;loop back
  249.  
  250. ;--- error reading
  251.  
  252. probyt5 cmp     ax, 0           ;check if EOF
  253.         jne     probyt7
  254.         or      InpSta, INP_EOF ;set end of file flag
  255.         or      cx, cx          ;check if any bytes
  256.         jz      probyt6
  257.         or      InpSta, INP_EOL ;set end of line also
  258. probyt6 ret
  259.         ret
  260.  
  261. probyt7 or      InpSta, INP_ERR ;set error flag
  262.         ret
  263.         ENDP                    ;Proc_Byte
  264.  
  265. ;------------------------------------------------
  266. ; Process low byte.
  267.  
  268. Byte_Low PROC   NEAR
  269.  
  270. ;--- end of line
  271.  
  272.         cmp     al, EOL         ;check if end of line
  273.         jne     bytlo1
  274.         or      InpSta, INP_EOL
  275.         ret
  276.  
  277. ;--- replace tabs with spaces
  278.  
  279. bytlo1  test    Options, REP_TAB ;test if replace tabs
  280.         jz      bytlo3
  281.         cmp     al, TAB         ;check if tab
  282.         jne     bytlo3
  283.         call    Tab_Next        ;get spaces needed
  284.         call    Spaces          ;store spaces
  285.         add     cx, dx          ;byte count
  286.         ret
  287.  
  288. ;--- carriage return
  289.  
  290. bytlo3  test    Options, SAV_CR  ;check if saving CR's
  291.         jnz     bytlo4
  292.         cmp     al, CR          ;check if carriage return
  293.         jne     bytlo4
  294.         ret
  295.  
  296. ;--- end of file
  297.  
  298. bytlo4  test    Options, SKP_EOF ;check if ignore EOF
  299.         jnz     bytlo6
  300.         cmp     al, EOF         ;check if end of page
  301.         jne     bytlo6
  302.         or      InpSta, INP_EOF ;set flag
  303.         or      cx, cx          ;check if any bytes
  304.         jz      bytlo5
  305.         or      InpSta, INP_EOL ;set end of line also
  306. bytlo5  ret
  307.  
  308. ;--- remove low bytes
  309.  
  310. bytlo6  test    Options, STR_LOB ;test if remove low bytes
  311.         jz      bytlo7
  312.         ret
  313.  
  314. ;--- write byte
  315.  
  316. bytlo7  stosb
  317.         inc     cx
  318.         ret
  319.         ENDP                    ;Byte_Low
  320.  
  321. ;------------------------------------------------
  322. ; Process normal byte.
  323.  
  324. Byte_Norm PROC  NEAR
  325.  
  326. ;--- end of line
  327.  
  328.         cmp     al, EOL         ;check if end of line
  329.         jne     bytnor1
  330.         or      InpSta, INP_EOL
  331.         ret
  332.  
  333. ;--- replace spaces with tabs
  334.  
  335. bytnor1 test    Options, REP_SPC ;test if replace spaces
  336.         jz      bytnor4
  337.  
  338. ;------ write tab stop
  339.  
  340.         call    Tab_Next        ;get next tab stop
  341.         jnc     bytnor2         ;jump if not at stop
  342.         call    Store_Tab       ;store a tab
  343.  
  344. ;------ space
  345.  
  346. bytnor2 cmp     al, ' '         ;check if really space
  347.         jne     bytnor3         ;jump if not
  348.         inc     SpcCnt          ;increment space count
  349.         inc     TabOff          ;increment column
  350.         ret
  351.  
  352. ;------ non-space
  353.  
  354. bytnor3 call    Store_Spc       ;store any spaces
  355.  
  356. ;--- convert to lower case
  357.  
  358. bytnor4 test    Options, MAK_LWR ;test if make lower case
  359.         jz      bytnor5
  360.         call    Lo_Case
  361.         stosb
  362.         inc     cx
  363.         ret
  364.  
  365. ;--- convert to upper case
  366.  
  367. bytnor5 test    Options, MAK_UPR ;test if make upper case
  368.         jz      bytnor6
  369.         call    Up_Case
  370.         stosb
  371.         inc     cx
  372.         ret
  373.  
  374. ;--- capitalize
  375.  
  376. bytnor6 test    Options, MAK_CAP ;test if capitalize
  377.         jz      bytnor8
  378.         test    InpSta, LAS_LET ;check if last was letter
  379.         jz      bytnor7
  380.         call    Lo_Case
  381.         stosb
  382.         inc     cx
  383.         ret
  384.  
  385. bytnor7 call    Up_Case
  386.         stosb
  387.         inc     cx
  388.         ret
  389.  
  390. ;--- write byte
  391.  
  392. bytnor8 stosb
  393.         inc     cx
  394.         ret
  395.         ENDP            ;Byte_Norm
  396.  
  397. ;------------------------------------------------
  398. ; Process high byte.
  399.  
  400. Byte_High PROC    NEAR
  401.  
  402. ;--- end of line
  403.  
  404.         cmp     al, EOL         ;check if end of line
  405.         jne     bythi1
  406.         or      InpSta, INP_EOL
  407.         ret
  408.  
  409. ;--- remove high bytes
  410.  
  411. bythi1  test    Options, STR_HIB ;test if remove high bytes
  412.         jz      bythi2
  413.         ret
  414.  
  415. ;--- write byte
  416.  
  417. bythi2  stosb
  418.         inc     cx
  419.         ret
  420.         ENDP                    ;Byte_High
  421.  
  422. ;================================================
  423. ; Process a line.
  424.  
  425. Proc_Line PROC  NEAR
  426.         sub     cx, cx                  ;clear bytes in buffer
  427.         lea     di, LinBuf              ;buffer location
  428.         mov     SpcCnt, 0               ;initial space count
  429.         mov     TabOff, 0               ;column offset
  430.         and     InpSta, NOT (LAS_LET OR INP_EOL)
  431.  
  432. ;--- loop for each byte in line
  433.  
  434. prolin1 call    Proc_Byte                       ;process byte
  435.  
  436.         test    InpSta, INP_EOL OR INP_EOF OR INP_ERR
  437.         jnz     prolin2
  438.  
  439.         test    Options, MAK_CAP                ;test if capitalize
  440.         jz      prolin1
  441.         or      cx, cx                          ;check if any input
  442.         jz      prolin1
  443.         and     InpSta, NOT LAS_LET             ;clear letter bit
  444.         mov     al, [di-1]                      ;get last character
  445.         call    Lo_Case                         ;make lower case
  446.         cmp     al, 'a'                         ;check if below a
  447.         jb      prolin1
  448.         cmp     al, 'z'                         ;check if above z
  449.         ja      prolin1
  450.         or      InpSta, LAS_LET                 ;set letter bit
  451.         jmps    prolin1
  452.  
  453. ;--- end of line
  454.  
  455. prolin2 test    InpSta, INP_EOL ;check if any line returned
  456.         jnz     prolin3
  457.         ret
  458.  
  459. ;--- extra trailing spaces
  460.  
  461. prolin3 lea     si, LinBuf      ;start of line data
  462.  
  463.         call    Tab_Next        ;get next tab stop
  464.         jnc     prolin4         ;jump if not at stop
  465.         call    Store_Tab       ;store a tab
  466.         jmps    prolin5
  467.  
  468. prolin4 call    Store_Spc       ;store any spaces
  469.  
  470. ;--- left margin
  471.  
  472. prolin5 mov     ax, LeftMar     ;get left margin
  473.         add     cx, ax          ;add to byte count
  474.         sub     si, ax          ;add margin
  475.  
  476. ;--- delete left margin characters
  477.  
  478.         mov     ax, LeftDel     ;margin remove
  479.         add     si, ax          ;skip beginning characters
  480.         sub     cx, ax          ;reduce count
  481.         jnc     prolin6         ;jump if okay (CX >= AX)
  482.         sub     cx, cx          ;set cx to zero
  483.  
  484. ;--- truncate line
  485.  
  486. prolin6 mov     ax, Trunc       ;truncate length
  487.         or      ax, ax          ;check if set
  488.         jz      prolin7
  489.         cmp     cx, ax
  490.         jbe     prolin7
  491.         mov     di, si          ;start of buffer
  492.         add     di, ax          ;line length
  493.         mov     cx, ax          ;byte count
  494.  
  495. ;--- remove trailing spaces
  496.  
  497. prolin7 test    Options, REM_SPC        ;check if remove
  498.         jz      prolin10
  499.  
  500. prolin8 or      cx, cx                  ;see if no bytes left
  501.         jz      prolin10
  502.         cmp     BYTE [di-1], ' '        ;check if space
  503.         je      prolin9
  504.         test    Options, REP_SPC        ;check if also delete tabs
  505.         jz      prolin10
  506.         cmp     BYTE [di-1], TAB        ;check if tab
  507.         jne     prolin10
  508.  
  509. prolin9 dec     di                      ;reduce pointer
  510.         dec     cx                      ;reduce count
  511.         jmps    prolin8
  512.  
  513. ;--- append CR+LF
  514.  
  515. prolin10 mov     al, CR
  516.         stosb                   ;store CR
  517.         inc     cx
  518.         mov     al, LF
  519.         stosb                   ;store LF
  520.         inc     cx              ;increment
  521.  
  522. ;--- write line
  523.  
  524.         lea     bx, OutBlk      ;output control block
  525.         call    File_Write      ;write to file
  526.         jnc     prolin11
  527.         or      InpSta, OUT_ERR ;set error flag
  528.  
  529. prolin11 ret
  530.         ENDP                    ;Proc_Line
  531.  
  532. ;================================================
  533. ; Process a document.
  534.  
  535. Proc_Doc  PROC  NEAR
  536.  
  537. ;--- process every line
  538.  
  539. propag1 call    Proc_Line
  540.         test    InpSta, INP_EOF OR INP_ERR OR OUT_ERR
  541.         jz      propag1
  542.  
  543. ;--- write EOF
  544.  
  545.         test    Options, SUP_EOF ;check if suppressed
  546.         jnz     propag2
  547.         test    InpSta, INP_ERR ;
  548.         jnz     propag2         ;-- check for errors
  549.         test    InpSta, OUT_ERR ;
  550.         jnz     propag2
  551.  
  552.         mov     cx, 1           ;bytes to write
  553.         lea     si, LinBuf      ;put it in line buffer
  554.         mov     BYTE [si], EOF  ;EOF character
  555.         lea     bx, OutBlk      ;output control block
  556.         call    File_Write      ;write to file
  557.  
  558. propag2 ret
  559.         ENDP                    ;Proc_Page
  560.